package Limit.BuffDoll;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.utils.SQLUtil;

public class BuffDoll_SkillList_Table {
	
	private static BuffDoll_SkillList_Table _instance;
	
	private final HashMap<Integer, BuffDoll_SkillList_Load> _SkillsListID = new HashMap<Integer, BuffDoll_SkillList_Load>();

	public static BuffDoll_SkillList_Table getInstance() {
		if (_instance == null) {
			_instance = new BuffDoll_SkillList_Table();
		}
		return _instance;
	}

	private BuffDoll_SkillList_Table() {
		listsID();
	}
	
	private void listsID(){
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("select * from buffdoll_skill_list");

			rs = pstm.executeQuery();
			while (rs.next()) {
				BuffDoll_SkillList_Load list = new BuffDoll_SkillList_Load();
				int id = rs.getInt("BuffID");
				list.setID(id);
				list.setIMG(rs.getInt("IMG"));
				list.setName(rs.getString("Name"));
				list.setTime(rs.getInt("Time"));
				list.setType(rs.getInt("Type"));
				list.setHtml_IMG(rs.getInt("Html_IMG"));
				list.setLearn_Item(rs.getInt("Learn_Item"));
				
				_SkillsListID.put(new Integer(id), list);
			}		
		}
		catch (SQLException e) {
			System.out.print(e.getMessage());
		}
		finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public int[] listsType(int type){
		int count = loadCount(type);
		int[] idlist = new int[count];
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		int i = 0;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("select * from buffdoll_skill_list WHERE Type = ? OR Type = 14 Order By ID ASC");
			pstm.setInt(1, type);
			rs = pstm.executeQuery();
			while (rs.next()) {
				idlist[i] = rs.getInt("Html_IMG");
				i++;
			}	
		}
		catch (SQLException e) {
			System.out.print(e.getMessage());
		}
		finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		return idlist;
	}
	
	public static int loadCount(int type)
	{
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		int i = 0;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM buffdoll_skill_list WHERE Type = ? OR Type = 14  Order By ID ASC");
			pstm.setInt(1, type);
			rs = pstm.executeQuery();
			
			while (rs.next())
			{
				i++;
			}
		}
		
		catch (SQLException e) 
		{
			System.out.print(e.getMessage());
		}
		finally 
		{
			SQLUtil.close(rs, pstm, con);
		}	
		return i;
	}
	
	public int loadSkillId(int type, int id){
		int skillid = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("select * from buffdoll_skill_list WHERE ID = ? AND (Type = ? OR Type = 14) Order By ID ASC");
			pstm.setInt(1, id);
			pstm.setInt(2, type);
			rs = pstm.executeQuery();
			while (rs.next()) {
				skillid = rs.getInt("BuffID");
			}	
		}
		catch (SQLException e) {
			System.out.print(e.getMessage());
		}
		finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		return skillid;
	}
	
	public BuffDoll_SkillList_Load getTemplateID(int ID) {
		if (_SkillsListID.containsKey(ID)) {
			return _SkillsListID.get(ID);
		}
		return null;
	}
	
}
